package com.zhongkai.service.impl;

import com.mysql.jdbc.StringUtils;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.app.service.PowerSyetemService;
import com.zhongkai.jedis.JedisClient;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.Chinaset;
import com.zhongkai.model3.MenusRole;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;

@Service
@Transactional(rollbackFor = Exception.class)
public class PowerSyetemServiceImpl implements PowerSyetemService {

    @Autowired
    private IBaseDao iBaseDao;

    @Autowired
    private JedisClient jedisClient;


    //根据用户的权限ID查询权限列表
    @Override
    public ZhongkaiResult queryRole(String role_id) {



        if (StringUtils.isNullOrEmpty(role_id)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        ArrayList<MenusRole> list = (ArrayList<MenusRole>) iBaseDao.find("from MenusRole tb where tb.id='" + role_id + "'");  //查询数据库

        if (list == null) {
            return ZhongkaiResult.build(400, "查询失败,请联系管理员");
        } else {
            try {


                return ZhongkaiResult.ok(list);                        //传给表现层
            } catch (Exception e) {
                e.printStackTrace();
                return ZhongkaiResult.build(400, "转换失败,请联系管理员");
            }


        }

        //  @JsonBackReference

/*
        String roleJson = jedisClient.get("ROLE:" + role_id); //查询缓存
        ObjectMapper mapper = new ObjectMapper();

        if (StringUtils.isNullOrEmpty(roleJson)) { //缓存不存在

            ArrayList<MenusRole> list = (ArrayList<MenusRole>) iBaseDao.find("from MenusRole tb where tb.id='" + role_id + "'");  //查询数据库

            if (list == null) {
                return ZhongkaiResult.build(400, "查询失败,请联系管理员");
            } else {
                try {
                   // mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, Boolean.TRUE);  //把list对象转换成json
                    String listJson = mapper.writeValueAsString(list);
                  //  System.out.println(listJson);
                    jedisClient.set("ROLE:" + role_id, listJson);          //存入缓存

                    return ZhongkaiResult.ok(list);                        //传给表现层
                } catch (Exception e) {
                    e.printStackTrace();
                    return ZhongkaiResult.build(400, "转换失败,请联系管理员");
                }


            }

        } else { //缓存存在 取出缓存
           // System.out.println(roleJson);
            //  ArrayList<MenusRole> menusList = mapper.readValues(roleJson,new TypeReference< ArrayList<MenusRole>>(){});
            try {
                ArrayList<MenusRole> menusList = mapper.readValue(roleJson, new TypeReference<List<MenusRole>>() {    //转成list
                });

                return ZhongkaiResult.ok(menusList);  //传给表现层
            } catch (Exception e) {
                e.printStackTrace();
                return ZhongkaiResult.build(400, "转换失败,请联系管理员");
            }

        }*/


    }

    @Override
    public ZhongkaiResult getUserToken(String token) {
        //Jedis jedis = new Jedis("192.168.25.128", 6379);
        String json = null;
        try {
            json = jedisClient.get(token);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(500, "redis连接获取失败");
        }

        if (StringUtils.isNullOrEmpty(json)) {
            //jedis.close();
            return ZhongkaiResult.build(201, "用户过期,请重新登录");
        } else {



            try {
                Chinaset chinaset;
                ObjectMapper mapper = new ObjectMapper();

                chinaset = mapper.readValue(json, Chinaset.class);
                try {
                    jedisClient.expire(token, 604800);//重新设置过期时间
                } catch (Exception e) {
                    e.printStackTrace();
                    return ZhongkaiResult.build(500, "redis连接获取失败");
                }

                return ZhongkaiResult.ok(chinaset);
            } catch (IOException e) {
                e.printStackTrace();
                return ZhongkaiResult.build(201, "查询失败,请重新登录");
            }

        }



        //System.out.println(json);
        //TbUser user =new Gson().fromJson(json,TbUser.class);

    }

    @Override
    public ZhongkaiResult getUserToken2(String token) {

        String json = null;
        try {
            json = jedisClient.get(token);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(500, "redis连接获取失败");
        }

        if (StringUtils.isNullOrEmpty(json)) {
            //jedis.close();
            return ZhongkaiResult.build(3002, "用户过期,请重新登录");
        } else {



            try {
/*                AppUser appUser;
                ObjectMapper mapper = new ObjectMapper();

                appUser = mapper.readValue(json, AppUser.class);*/
                jedisClient.expire(token, 604800);//重新设置过期时间

                return ZhongkaiResult.ok();
            } catch (Exception e) {
                e.printStackTrace();
                return ZhongkaiResult.build(3001, "请重新登录");
            }

        }
    }
}
